package com.wuyou.mid;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 667. 优美的排列 II
 */
public class ConstructArray {


    public static void main(String[] args) {
        ConstructArray constructArray = new ConstructArray();
        System.out.println(constructArray.solution_2(5, 3));
    }


    public int[] solution_2(int n, int k) {
        int[] answer = new int[n];
        int leftNum = 1;
        int rightNum = k + 1;
        for (int i = 0; i < n; i++) {
            answer[i] = leftNum;

            if (i < k && i % 2 == 0) {
                i++;
                answer[i] = rightNum;
                rightNum--;
            }

            leftNum ++;
        }
        return answer;
    }

    public int[] solution_1(int n, int k) {
        int[] answer = new int[n];
        for (int i = 0; i < n; i++) {
            answer[i] = i + 1;
        }
        int rightNum = k;
        int leftNum = 1;
        while (leftNum < k) {
            backwardArray(answer, leftNum, answer[k], k);
            leftNum += 2;
        }
        return answer;
    }

    public void backwardArray(int[] arr, int index, int num, int k) {
        int temp = arr[index];
        arr[index] = num;
        for (int i = index + 1; i < arr.length; i++) {
            int tempInt = arr[i];
            arr[i] = temp;
            temp = tempInt;
            if (i == k) {
                break;
            }
        }
    }
}
